﻿

(function ($) {
    $.fn.findHtml = function (content) {
        var obj;
        this.each(function (i, d) {
            if ($(d).html() == content) {
                obj = d;
                return false;
            }

        });

        return $(obj);
    };

    $.fn.GetDivJson = function (prefix, option) {
        prefix = prefix || "";

        var p = $.extend({ valueProc: function (val) { return (val || "").trim(); } }, option);

        var idValues = {}, nameValues = {},
	            getKey = function (key) {
	                if (key.indexOf(prefix) != 0) return;
	                return key.slice(prefix.length);
	            };
        this.each(function (_i, _d) {
            $(_d).find(":text,textarea,:hidden,:password,:radio:checked").each(function (oi, d) {
                if (d.name) {
                    var nameKey = getKey(d.name);
                    if (nameKey) {
                        nameValues[nameKey] = p.valueProc($(d).val());
                    }
                }
                else if (d.id) {
                    var idKey = getKey(d.id);
                    if (idKey) {
                        idValues[idKey] = p.valueProc($(d).val());
                    }
                }
            });

            $(_d).find(":selected").each(function (oi, d) {
                var nameKey = getKey($(d).parent()[0].name);
                if (nameKey) {
                    nameValues[nameKey] = p.valueProc($(d).val());
                }
            });

            for (var key in nameValues) {
                idValues[key] = nameValues[key];
            }

            var checkValues = {};
            $(_d).find(":checkbox:checked").each(function (oi, od) {
                var nm = od.name || od.id;
                nm = getKey(nm);
                if (nm) {
                    if (!checkValues[nm]) checkValues[nm] = [];
                    checkValues[nm].push(p.valueProc($(od).val()));
                }
            });

            for (var key in checkValues) {
                $(checkValues[key]).each(function (i, d) {

                    idValues[key + "[" + i + "]"] = d;
                });
            }
        });
        return idValues;
    };


    $.fn.SetDivJson = function (prefix, JsonValue) {
        prefix = prefix || "";

        var setValue = function (jObj, value) {
            var tagName = jObj[0].tagName.toLowerCase();
            if ($.inArray(tagName, "input,select,textarea,select".split(",")) >= 0) {
                if ((jObj.attr("type") || "").toLowerCase() == "checkbox") {
                    if ($.inArray(value, "on", "true")) {
                        jObj.attr("checked", "checked");
                    }
                    else {
                        jObj.removeAttr("checked");
                    }
                }
                else {
                    jObj.val(jv.decode(value));
                }
            }

            else {
                jObj.html(value);
            }
        };
        var container = this;
        if (container.length == 0) { container = $("body"); }
        container.find(":checkbox").removeAttr("checked");

        container.each(function (_i, _d) {
            for (var jp in JsonValue) {
                var p = jp.replace(/\$/g, "\\$").replace(/\#/g, "\\#");

                var leftKIndex = p.indexOf('['), rightKIndex = p.indexOf(']'),
                    isCheck = (p.slice(-1) == ']' && (leftKIndex < rightKIndex));
                var checkIndex = -1;
                if (isCheck) {
                    checkIndex = parseInt(p.slice(leftKIndex + 1, rightKIndex));
                    p = p.slice(0, leftKIndex);
                }

                var jObj = $("[name=" + prefix + p + "]");
                if (!jObj.length) {
                    jObj = $("#" + prefix + p);
                }

                if (!jObj.length) {
                    jObj = $("[name*=" + prefix + p + "]");
                }

                if (!jObj.length) {
                    jObj = $("[id*=" + prefix + p + "]");
                }

                if (jObj.length) {
                    if (isCheck) {
                        setValue(jObj.eq(checkIndex), JsonValue[jp]);
                    }
                    else {
                        setValue(jObj, JsonValue[jp]);
                    }
                }
            }
        });
    };
})($);


//用JS给表单的元素赋值
//注意！本程序不支持Name相同但Type不同的表单元素
//name:表单元素name；value:值；form:表单
function setFormValue(name, value) {
    var name, value, type, Obj, i, x;
    var FormObj = document.getElementById(arguments[2]) ? document.getElementById(arguments[2]) : null;
    Obj = (FormObj == null) ? document.getElementsByName(name) : eval('FormObj["' + name + '"]');
    if (Obj != undefined) {
        //判断表单项类型
        if (FormObj == null) {
            if (Number(Obj.length) == 0) return false;
            type = Obj[0].type;
        } else {
            if (Obj[0] != undefined) {
                type = Obj[0].type;
                if (type == undefined) { type = Obj.type; Obj = new Array(Obj); }
            } else {
                type = Obj.type;
                Obj = new Array(Obj);
            }
        }
        //向表单填值
        if (type == "radio") {
            for (i = 0; i < Obj.length; i++) {
                if (Obj[i].value == value) {
                    Obj[i].checked = true;
                }
            }
        } else if (type == "checkbox") {
            value = "," + value + ",";
            for (i = 0; i < Obj.length; i++) {
                if (value.indexOf("," + Obj[i].value + ",") > -1) {
                    Obj[i].checked = true;
                }
            }
        } else if (type == 'select-multiple') {
            value = value.split(',');
            for (i = 0; i < value.length; i++) {
                for (x = 0; x < Obj[0].options.length; x++) {
                    if (Obj[0].options[x].value == value[i].replace(/^\s*(.*)\s*$/g, '$1')) {
                        Obj[0].options[x].selected = true;
                    }
                }
            }
        } else if (type == "textarea") {
            var frameId = "";
            for (i = 0; i < document.getElementsByTagName("iframe").length; i++) {
                if (document.getElementsByTagName("iframe")[i].src.indexOf(name) >= 0) {
                    frameId = document.getElementsByTagName("iframe")[i].id;
                }
            }
            if (frameId == "") {//普通Textarea
                Obj[0].value = value;
            } else {//eWebEditor
                eval(frameId + '.setHTML(value)');
            }
        } else {
            Obj[0].value = value;
        }
    }
}


